package com.gmall.cart.controller;

import com.gmall.cart.service.CartService;
import com.gmall.common.result.Result;
import com.gmall.model.cart.CartInfo;
import com.gmall.model.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cart")
public class CartController {


    @Autowired
    CartService cartService;

    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result<String> addToCart(@PathVariable Long skuId,
                                    @PathVariable Integer skuNum,
                                    @RequestHeader Long userId){
        // 调用服务层方法，将商品添加到购物车
         return cartService.addToCart(userId, skuId, skuNum);
        // 返回成功或失败信息
    }

    @GetMapping("cartList")
    public Result<List<CartInfo>> cartList(@RequestHeader Long userId){
        return cartService.cartList(userId);
    }

    @PostMapping("cartPage/{pageNo}/{pageSize}")
    public Result<PageResult<CartInfo>> cartPage(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            @RequestBody Map sortFieldMap,
            @RequestHeader Long userId){
        return cartService.cartPage(userId,pageNo,pageSize,sortFieldMap);
    }

    @GetMapping("deleteCart/{skuId}")
    public Result<String> deleteCart(@PathVariable Long skuId,
                                     @RequestHeader Long userId){
        return cartService.deleteCart(userId,skuId);
    }
}
